{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>START</th>\n",
       "      <th>STOP</th>\n",
       "      <th>PATIENT</th>\n",
       "      <th>PAYER</th>\n",
       "      <th>ENCOUNTER</th>\n",
       "      <th>CODE</th>\n",
       "      <th>DESCRIPTION</th>\n",
       "      <th>BASE_COST</th>\n",
       "      <th>PAYER_COVERAGE</th>\n",
       "      <th>DISPENSES</th>\n",
       "      <th>TOTALCOST</th>\n",
       "      <th>REASONCODE</th>\n",
       "      <th>REASONDESCRIPTION</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2012-03-01T06:14:03Z</td>\n",
       "      <td>2012-03-22T06:14:03Z</td>\n",
       "      <td>7d3e489a-7789-9cd6-2a1b-711074af481b</td>\n",
       "      <td>d47b3510-2895-3b70-9897-342d681c769d</td>\n",
       "      <td>d989eaad-4105-1f1b-62f3-bb15e024ec2c</td>\n",
       "      <td>562251</td>\n",
       "      <td>Amoxicillin 250 MG / Clavulanate 125 MG Oral T...</td>\n",
       "      <td>24.26</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1</td>\n",
       "      <td>24.26</td>\n",
       "      <td>444814009.0</td>\n",
       "      <td>Viral sinusitis (disorder)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2013-05-08T00:17:55Z</td>\n",
       "      <td>2013-05-19T00:17:55Z</td>\n",
       "      <td>3829c803-1f4c-74ed-0d8f-36e502cadd0f</td>\n",
       "      <td>6e2f1a2d-27bd-3701-8d08-dae202c58632</td>\n",
       "      <td>a4f8f0e3-a654-96ec-f154-a95119683058</td>\n",
       "      <td>834061</td>\n",
       "      <td>Penicillin V Potassium 250 MG Oral Tablet</td>\n",
       "      <td>32.88</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1</td>\n",
       "      <td>32.88</td>\n",
       "      <td>43878008.0</td>\n",
       "      <td>Streptococcal sore throat (disorder)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2013-11-02T00:43:55Z</td>\n",
       "      <td>2014-01-01T00:43:55Z</td>\n",
       "      <td>3829c803-1f4c-74ed-0d8f-36e502cadd0f</td>\n",
       "      <td>6e2f1a2d-27bd-3701-8d08-dae202c58632</td>\n",
       "      <td>ef0554c1-eca1-9f75-b5d3-44b432766db9</td>\n",
       "      <td>313820</td>\n",
       "      <td>Acetaminophen 160 MG Chewable Tablet</td>\n",
       "      <td>6.18</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2</td>\n",
       "      <td>12.36</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2018-05-22T06:14:03Z</td>\n",
       "      <td>2018-05-22T06:14:03Z</td>\n",
       "      <td>7d3e489a-7789-9cd6-2a1b-711074af481b</td>\n",
       "      <td>d47b3510-2895-3b70-9897-342d681c769d</td>\n",
       "      <td>87cae904-0417-df2f-99ac-65baf83cc201</td>\n",
       "      <td>896209</td>\n",
       "      <td>60 ACTUAT Fluticasone propionate 0.25 MG/ACTUA...</td>\n",
       "      <td>25.67</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1</td>\n",
       "      <td>25.67</td>\n",
       "      <td>185086009.0</td>\n",
       "      <td>Chronic obstructive bronchitis (disorder)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2018-05-22T06:14:03Z</td>\n",
       "      <td>2019-05-28T06:14:03Z</td>\n",
       "      <td>7d3e489a-7789-9cd6-2a1b-711074af481b</td>\n",
       "      <td>d47b3510-2895-3b70-9897-342d681c769d</td>\n",
       "      <td>87cae904-0417-df2f-99ac-65baf83cc201</td>\n",
       "      <td>896209</td>\n",
       "      <td>60 ACTUAT Fluticasone propionate 0.25 MG/ACTUA...</td>\n",
       "      <td>34.82</td>\n",
       "      <td>0.0</td>\n",
       "      <td>12</td>\n",
       "      <td>417.84</td>\n",
       "      <td>185086009.0</td>\n",
       "      <td>Chronic obstructive bronchitis (disorder)</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                  START                  STOP  \\\n",
       "0  2012-03-01T06:14:03Z  2012-03-22T06:14:03Z   \n",
       "1  2013-05-08T00:17:55Z  2013-05-19T00:17:55Z   \n",
       "2  2013-11-02T00:43:55Z  2014-01-01T00:43:55Z   \n",
       "3  2018-05-22T06:14:03Z  2018-05-22T06:14:03Z   \n",
       "4  2018-05-22T06:14:03Z  2019-05-28T06:14:03Z   \n",
       "\n",
       "                                PATIENT                                 PAYER  \\\n",
       "0  7d3e489a-7789-9cd6-2a1b-711074af481b  d47b3510-2895-3b70-9897-342d681c769d   \n",
       "1  3829c803-1f4c-74ed-0d8f-36e502cadd0f  6e2f1a2d-27bd-3701-8d08-dae202c58632   \n",
       "2  3829c803-1f4c-74ed-0d8f-36e502cadd0f  6e2f1a2d-27bd-3701-8d08-dae202c58632   \n",
       "3  7d3e489a-7789-9cd6-2a1b-711074af481b  d47b3510-2895-3b70-9897-342d681c769d   \n",
       "4  7d3e489a-7789-9cd6-2a1b-711074af481b  d47b3510-2895-3b70-9897-342d681c769d   \n",
       "\n",
       "                              ENCOUNTER    CODE  \\\n",
       "0  d989eaad-4105-1f1b-62f3-bb15e024ec2c  562251   \n",
       "1  a4f8f0e3-a654-96ec-f154-a95119683058  834061   \n",
       "2  ef0554c1-eca1-9f75-b5d3-44b432766db9  313820   \n",
       "3  87cae904-0417-df2f-99ac-65baf83cc201  896209   \n",
       "4  87cae904-0417-df2f-99ac-65baf83cc201  896209   \n",
       "\n",
       "                                         DESCRIPTION  BASE_COST  \\\n",
       "0  Amoxicillin 250 MG / Clavulanate 125 MG Oral T...      24.26   \n",
       "1          Penicillin V Potassium 250 MG Oral Tablet      32.88   \n",
       "2               Acetaminophen 160 MG Chewable Tablet       6.18   \n",
       "3  60 ACTUAT Fluticasone propionate 0.25 MG/ACTUA...      25.67   \n",
       "4  60 ACTUAT Fluticasone propionate 0.25 MG/ACTUA...      34.82   \n",
       "\n",
       "   PAYER_COVERAGE  DISPENSES  TOTALCOST   REASONCODE  \\\n",
       "0             0.0          1      24.26  444814009.0   \n",
       "1             0.0          1      32.88   43878008.0   \n",
       "2             0.0          2      12.36          NaN   \n",
       "3             0.0          1      25.67  185086009.0   \n",
       "4             0.0         12     417.84  185086009.0   \n",
       "\n",
       "                           REASONDESCRIPTION  \n",
       "0                 Viral sinusitis (disorder)  \n",
       "1       Streptococcal sore throat (disorder)  \n",
       "2                                        NaN  \n",
       "3  Chronic obstructive bronchitis (disorder)  \n",
       "4  Chronic obstructive bronchitis (disorder)  "
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#Package Imports\n",
    "import pandas as pd\n",
    "\n",
    "#Reading in the dataset\n",
    "data = pd.read_csv(\"../data/medications.csv\")\n",
    "df = data\n",
    "\n",
    "#Viewing the first 5 rows of the dataset\n",
    "\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 250620 entries, 0 to 250619\n",
      "Data columns (total 13 columns):\n",
      " #   Column             Non-Null Count   Dtype  \n",
      "---  ------             --------------   -----  \n",
      " 0   START              250620 non-null  object \n",
      " 1   STOP               236572 non-null  object \n",
      " 2   PATIENT            250620 non-null  object \n",
      " 3   PAYER              250620 non-null  object \n",
      " 4   ENCOUNTER          250620 non-null  object \n",
      " 5   CODE               250620 non-null  int64  \n",
      " 6   DESCRIPTION        250620 non-null  object \n",
      " 7   BASE_COST          250620 non-null  float64\n",
      " 8   PAYER_COVERAGE     250620 non-null  float64\n",
      " 9   DISPENSES          250620 non-null  int64  \n",
      " 10  TOTALCOST          250620 non-null  float64\n",
      " 11  REASONCODE         202454 non-null  float64\n",
      " 12  REASONDESCRIPTION  202454 non-null  object \n",
      "dtypes: float64(4), int64(2), object(7)\n",
      "memory usage: 24.9+ MB\n"
     ]
    }
   ],
   "source": [
    "df.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Drop rows with any empty cells\n",
    "df.dropna(\n",
    "    axis=0,\n",
    "    how='any',\n",
    "    thresh=None,\n",
    "    subset=None,\n",
    "    inplace=True\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = df.select_dtypes(exclude=['object'])\n",
    "data = df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "           CODE  BASE_COST  PAYER_COVERAGE  DISPENSES  TOTALCOST   REASONCODE\n",
      "0        562251      24.26            0.00          1      24.26  444814009.0\n",
      "1        834061      32.88            0.00          1      32.88   43878008.0\n",
      "3        896209      25.67            0.00          1      25.67  185086009.0\n",
      "4        896209      34.82            0.00         12     417.84  185086009.0\n",
      "5        313782       6.62            0.00          1       6.62   10509002.0\n",
      "...         ...        ...             ...        ...        ...          ...\n",
      "250612  2123111     120.79           80.79         12    1449.48  233678006.0\n",
      "250613   896209      39.72            0.00         12     476.64  185086009.0\n",
      "250614   895994      48.85            8.85         12     586.20  233678006.0\n",
      "250615  2123111     403.47          363.47         12    4841.64  233678006.0\n",
      "250616   896209      42.70            2.70         12     512.40  185086009.0\n",
      "\n",
      "[195032 rows x 6 columns]\n"
     ]
    }
   ],
   "source": [
    "print(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/pavlito/miniconda3/envs/pyvertical-dev/lib/python3.7/site-packages/ipykernel_launcher.py:2: UserWarning: Pandas doesn't allow columns to be created via a new attribute name - see https://pandas.pydata.org/pandas-docs/stable/indexing.html#attribute-access\n",
      "  \n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "data.data = np.array(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[5.62251000e+05 2.42600000e+01 0.00000000e+00 1.00000000e+00\n",
      "  2.42600000e+01 4.44814009e+08]\n",
      " [8.34061000e+05 3.28800000e+01 0.00000000e+00 1.00000000e+00\n",
      "  3.28800000e+01 4.38780080e+07]\n",
      " [8.96209000e+05 2.56700000e+01 0.00000000e+00 1.00000000e+00\n",
      "  2.56700000e+01 1.85086009e+08]\n",
      " ...\n",
      " [8.95994000e+05 4.88500000e+01 8.85000000e+00 1.20000000e+01\n",
      "  5.86200000e+02 2.33678006e+08]\n",
      " [2.12311100e+06 4.03470000e+02 3.63470000e+02 1.20000000e+01\n",
      "  4.84164000e+03 2.33678006e+08]\n",
      " [8.96209000e+05 4.27000000e+01 2.70000000e+00 1.20000000e+01\n",
      "  5.12400000e+02 1.85086009e+08]]\n"
     ]
    }
   ],
   "source": [
    "print(data.data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Store the feature data\n",
    "X = data.data\n",
    "# store the target data\n",
    "y = data.REASONCODE\n",
    "# split the data using Scikit-Learn's train_test_split\n",
    "from sklearn.model_selection import train_test_split\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9999589810902826"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.neighbors import KNeighborsClassifier\n",
    "logreg = KNeighborsClassifier(n_neighbors=6)\n",
    "logreg.fit(X_train, y_train)\n",
    "logreg.score(X_test, y_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
